/**
 * Created by baiguiren on 2/16/17.
 */

let redis = require('../common/redis');
let config = require('../config');

//const cache_prefix = config.redis.prefix;

//保存对应用户的session key
//merchant_41: node_shells:(session ID)
let setMerchantSessionKey = function (merchant_id, session_id) {
  //let cache_session_key = cache_prefix + session_id;
  let cache_key = getCacheMerchantKey(merchant_id);
  redis.set(cache_key, session_id, function (err, reply) {});
};
exports.setMerchantSessionKey = setMerchantSessionKey;

let getCacheMerchantKey = function (merchant_id) {
  //这里返回的key保存用户session ID(不带前缀)
  return 'merchant_' + merchant_id;
};

let kickMerchantFromSession = function (merchant_id) {
  let cache_key = getCacheMerchantKey(merchant_id);
  return new Promise(function (resolve, reject) {
    redis.get(cache_key, function (err, cache_session_key) {
      redis.del(cache_session_key);
      redis.del(cache_key);
      resolve();
    });
  })
};
exports.kickMerchantFromSession = kickMerchantFromSession;